
let PrefabUtility = {};
PrefabUtility.savePrefabData = (data) => {
    fetch('/api/savePrefab', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    }).then((res) => {
        res.json().then(data => {
            console.log(data)
        })
    })
}
PrefabUtility.savePrefabAsset = (prefabRoot, prefabOptions = {}) => {
    if(!prefabRoot || !prefabRoot.isBaseObject){
        console.error("you should assign an Object to prefabRoot before export!!!");
        return;
    }
    if(prefabRoot.isBaseModelObject3D){
        console.warn("the prefabRoot can not be BaseModelObject3D!!!");
    }
    
    let exporter = new THING.SceneExporter(prefabOptions);
    exporter.addExtensions({
        isPrefab: true
    });

    exporter.addObjects(prefabRoot);
    let data = exporter.export();
    PrefabUtility.savePrefabData(data);
}

function registerRefresh(){
    fetch('/api/watchPort', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: ""
    }).then((res) => {
        res.json().then(data => {
            if(data.port){
                let url = 'ws://localhost:' + data.port;
                const socket = new WebSocket(url);
                socket.addEventListener('message', event => {
                  location.reload();
                });
            }
        })
    })
}
registerRefresh();


